package com.taotao.manage.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.bean.EasyUIResult;
import com.taotao.manage.pojo.Content;
import com.taotao.manage.service.ContentService;

@Controller
@RequestMapping("/content")
public class ContentController {

	@Autowired
	ContentService contentService;
	
	/**
	 * categoryId=0&page=1&rows=20
	 * 根据内容分类分页查询content
	 * 异步
	 * 参数: page, rows
	 * 返回结果: total, list<content> ==> EasyUIResult
	 * 
	 */
	
	@GetMapping
	public ResponseEntity<EasyUIResult> queryContentPageListByContentCategoryId(
			@RequestParam("categoryId") Long categoryId, 
			@RequestParam(value="page", defaultValue="1") Integer page, 
			@RequestParam(value="rows", defaultValue="10")Integer rows){
		try {
			Content record = new Content();
			record.setCategoryId(categoryId);
			PageInfo<Content> pageInfo = contentService.queryPageListByWhere(record, page, rows);
			if(pageInfo != null ){
				EasyUIResult result = new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
				return ResponseEntity.status(HttpStatus.OK).body(result);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
	}
	
	/**
	 * 新增content
	 * 异步
	 * 参数: content对象
	 * 返回结果: void 201 500
	 * 
	 */
	
	@PostMapping
	public ResponseEntity<Void> saveContent(Content content){
		try {
			Boolean flag = contentService.save(content);
			if(flag){
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
	
	
	
}



















